package cn.hellochaos.generator.controller;

import cn.hellochaos.generator.entity.dto.ResultBean;

import com.wu.demo.fileupload.demo.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Map;

@RestController
@RequestMapping("/fishery/api/v1/profile")
public class profileController {

    private final ResourceLoader resourceLoader;

    @Autowired
    public profileController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Value("${web.upload-path}")
    private String path;

    /**
     *
     * @param file 要上传的文件
     * @return
     */
    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
    public String upload(@RequestParam("fileName") MultipartFile file){

        // 要上传的目标文件存放路径
        String localPath = "/usr/local/nginx-fishery-blockchain/profilepicture";
        // 上传成功或者失败的提示
        String msg = "";
        // 上传成功或者失败的提示
        if(FileNameUtils.getSuffix(file.getOriginalFilename()).contains(".jpg")||FileNameUtils.getSuffix(file.getOriginalFilename()).contains(".png")) {
            if (FileUtils.upload(file, localPath, file.getOriginalFilename())){
                // 上传成功，给出页面提示
                msg = "上传成功！";
            }else {
                msg = "上传失败！";

            }
        }
        else{
            msg="上传格式错误";
        }
        return msg;
    }

    /**
     * 显示单张图片
     * @return
     */
   @RequestMapping(value = "/show",method = RequestMethod.GET)
    public ResponseEntity<byte[]> showPhotos(String fileName){
       
        try {
            File file = new File(path+fileName);
            // 设置一个head
            HttpHeaders headers = new HttpHeaders();
            if(FileNameUtils.getSuffix(fileName).contains(".jpg")) {
                headers.setContentType(MediaType.IMAGE_JPEG);//设置ContentType的值 IMAGE_JPEG在浏览器返回图片
            }
            else if(FileNameUtils.getSuffix(fileName).contains(".png")) {
                headers.setContentType(MediaType.IMAGE_PNG);
            }
            else{
                String string="格式不支持";
                byte[] bytes=string.getBytes();
                headers.setContentType(MediaType.TEXT_EVENT_STREAM);
                return new ResponseEntity<byte[]>(bytes,headers,HttpStatus.NOT_FOUND);
            }
            // 内容是字节流
            byte[] bytes = FileUtils.readFileToByteArray(file);
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
           return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

}
